//
//  YHConfirmPopView.swift
//  FuweiCloud
//
//  Created by michael on 2024/10/16.
//

import UIKit
#if canImport(SnapKit)
import SnapKit
#endif

@objcMembers
open class YHConfirmPopView: YHToastPopView {
  public var buttonSpacing: CGFloat = 0
  public var leftButtonWidth: CGFloat = 0
    
  private(set) lazy var cancelButton: UIButton = {
    let button = UIButton.title(leftButton!, action: .buttonAction, target: self)
    return button
  }()
    
  public override var touchOutside: Bool {
    if direction == .center && properties?[.touchOutside] == nil {
      false
    } else {
      super.touchOutside
    }
  }
    
  private(set) lazy var confirmButton: UIButton = .title(rightButton!, action: .buttonAction, target: self)
    
  private var leftButton: Any! = "Cancel".local
  private var rightButton: Any! = "Confirm".local
    
  public override func initContentUI() {
    if let properties {
      leftButton = properties[.leftButton] ?? "Cancel".local
      rightButton = properties[.rightButton] ?? "Confirm".local
      buttonSpacing = (properties[.buttonSpacing] as? CGFloat) ?? 0
      leftButtonWidth = (properties[.leftButtonWidth] as? CGFloat) ?? 0
    }
    let footer = UIView()
    footer.addSubviews([cancelButton, confirmButton])
#if canImport(SnapKit)
    cancelButton.snp.makeConstraints { make in
      make.left.top.bottom.equalTo(0)
      if leftButtonWidth != 0 {
        make.width.equalTo(leftButtonWidth)
      } else {
        make.right.equalTo(confirmButton.snp.left).offset(-buttonSpacing)
      }
    }
    confirmButton.snp.makeConstraints { make in
      make.right.top.bottom.equalTo(0)
      if leftButtonWidth != 0 {
        make.left.equalTo(cancelButton.snp.right).offset(buttonSpacing)
      } else {
        make.width.equalTo(cancelButton.snp.width)
      }
    }
#endif
    self.footer = footer
    fitHeight = true
    footerPadding = 20
    topPadding = 20
    super.initContentUI()
  }
    
  public func buttonAction(_ btn: UIButton) {
    dismiss(btn == cancelButton ? nil : 1)
  }
}
